/**
 * Copyright (C) 2010 Daniel <daniel.petisme@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * 
 */
package fr.daniel.todolist.view.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

import fr.daniel.todolist.model.pojo.Todo;


/**
 * @author Daniel
 *
 */
public class TodoList{

	private static final String PERSISTENCE_UNIT_NAME = "todolist";
	@PersistenceUnit(name = "todolist")
	private EntityManagerFactory factory;
	@PersistenceContext
	private EntityManager em;
	
	
	/** Todos to display*/
	private List<Todo> todolist;
	
	/** The unique instance*/
	public static TodoList instance;
	
	/** The default constructor*/
	private TodoList(){
		todolist = new ArrayList<Todo>();
		factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		em = factory.createEntityManager();
			}

	/**
	 * The instance access
	 * @return the unique instance
	 */
	public static TodoList getInstance(){
		if(instance == null){
			instance = new TodoList();
		}
		return instance;
	}
	
	/**
	 *  Get All the Todo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Todo> getAllTodos(){
		todolist = em.createQuery("select t from Todo t order by t.label").getResultList();
		return todolist;
	}

}
